<?php
/**
 * Created by PhpStorm.
 * User: qiaoli
 * Date: 15/12/23
 * Time: 下午5:24
 */
class Feedm extends CI_Model
{
    function __construct()
    {
        $this->load->model('userm');
        $this->load->model('tmm');
        $this->load->Model("followm");
    }
    public function insert_feed($user_id, $content, $at_friend_list_id_array_str = "")
    {
        $query = array("author_id" => $user_id, "content" => $content, "at_friend_list_id_array" => $at_friend_list_id_array_str, "create_time" => time());
        $this->db->insert("ml_feed", $query);

        return $this->db->insert_id();
    }

    public function del_feed($feed_id) {
        $this->db->set('is_show', '0');
        $this->db->where(array("id"=>(int)$feed_id));
        $this->db->update('ml_feed');
        return 1;
    }
    public function one_feed_content($feed_id)
    {
        $this->db->select('ml_feed.*,ml_fellow.thread AS thread,ml_fellow.username AS author_name,ml_fellow.head_ico AS head_ico,ml_fellow.verified_status AS verified_status,ml_fellow.admin_level as admin_level,ml_fellow.user_level as user_level,ml_fellow.is_show_truename as is_show_truename,ml_fellow.truename as truename,ml_fellow.practice_hospital as practice_hospital, ml_fellow.post_title as post_title')->from('ml_feed');
        $this->db->join('ml_fellow', 'ml_feed.author_id=ml_fellow.id', "left");
        $this->db->where('ml_feed.id', $feed_id);
        $this->db->where('ml_feed.is_show', 1);
        $query = $this->db->get()->result_array();

        if (empty($query)) {
            return array();
        }

        $row = $query[0];

        $data_content = array();
        $data_content['id'] = $feed_id;
        $data_content['author_id'] = $row['author_id'];
        $data_content['verified_status'] = $row['verified_status'];

        if (!isset($this->threadm)) {
            $this->load->model('threadm');
        }
        $data_content["thread_name"] = $this->threadm->get_thread_name($row["thread"]);


        $data_content['admin_level'] = $row['admin_level'];
        $data_content['is_expert'] = $row['user_level'] ? 1 : 0;
        $data_content['content'] = $row['content'];
        $data_content['create_time'] = $row['create_time'];
        $data_content['author_name'] = $row['author_name'];
        $data_content['head_ico'] = ret_fullpath_url($row['head_ico']);

        // 观察者与被观察者为同一人时 返回 0
        if($row['author_id'] == $this->uid)
        {
            $follow_status = 0;
        }
        else
        {
            $follow_status = $this->followm->get_follow_status($this->uid, $row['author_id']);
        }
        $data_content['follow_status'] = $follow_status;

        //实名
        if($row['is_show_truename'] == 1)
        {
            if($row['truename'] && strcmp($row['truename'], $row['author_name']) != 0)
            {
                $data_content['author_name'] = $row['truename'].'「'.$row['author_name'].'」';
            }
        }

        //认证用户展示医院
        if($row['verified_status'] == 3)
        {
            //取认证医院
            $verified_info = $this->userm->verified_info($row['author_id']);
            $hospital =  $verified_info['hospital'];
            if(!$hospital)
            {
                $hospital = $row['practice_hospital'];
            }
            $data_content['practice_hospital'] = $hospital;
            if(is_numeric($row['post_title']))
            {
                ci_load_once('posttitlem');
                $row['post_title'] = $this->posttitlem->getPosttitleName($row['post_title']);
            }
            $data_content['thread_name'] = $data_content['thread_name'].' '.$row['post_title'];
        }

        return $data_content;
    }

    /**
     * @param $user_id
     * @param array $toids
     * @param $event_id
     * @param $content
     * @return bool
     */
    public function send_dongtai_at_friend_lst($user_id, $toids = array(), $event_id, $content) {
        if (empty($toids)) {
            return false;
        }
        $user_info = $this->userm->getUserInfoById($user_id);
        $title = $user_info["username"] . "在动态中提到了你";
        $this->load->model('messagem');
        $this->messagem->snd_dongtai($user_id, $toids, $event_id, $title, $content);
        return true;
    }
}